<?php
include 'connect_db.php';
if (!checkLoggedin())
{
	header('Location: login.php');
}

$sql = "SELECT *, B.id as bill_id FROM bills B, categories C
		WHERE B.user_id = " . $_SESSION['id'] .  " AND B.category_id = C.id ORDER BY create_time DESC";
$people_owe = mysql_query( $sql );

$sql = "SELECT *, B.user_id AS who_paid_id FROM bills B, bills_users A, users U, categories C 
		WHERE A.user_id = " . $_SESSION['id'] . " 
		AND A.bill_id = B.id
		AND U.id = B.user_id
		AND B.category_id = C.id
		AND if_paid != True
		ORDER BY create_time DESC";
$i_owe = mysql_query( $sql );

$sql = "SELECT SUM(B.amount) AS sum FROM bills B
		WHERE B.user_id = " . $_SESSION['id'];
$i_paid_sum = mysql_fetch_array(mysql_query( $sql ));

$sql = "SELECT SUM(B.amount*BU.shared_scale) AS sum FROM bills B, bills_users BU
		WHERE BU.user_id = " . $_SESSION['id'] . "
		AND if_paid != True
		AND BU.bill_id = B.id";
$i_owe_sum = mysql_fetch_array(mysql_query( $sql ));

?>

<?php include('templates/main/header.php'); ?>

<div id="body">
    <div id="content" class="right_sidebar">
	<div id="mybills">
		<hr>
		<h1>My Bills</h1>
		<hr>
		<br />
		<br />
		
	  <h3 class="title">I Owe: </h3>
	  <?php 
	  while ( $row = mysql_fetch_array($i_owe) ) {?>		
		<p class="amount-owed">$<?php echo ( round($row['amount'] * $row['shared_scale'], 1) ) ?></p>
		<p><img border="1px" src="uploads/
			<?php
				if (file_exists("uploads/" . md5($row['who_paid_id'].$row['username']).'.png'))
			    {
			      echo  md5($row['who_paid_id'].$row['username']);
			    }
			    else
			    {
			      echo 'zombie';
			    }
			?>
		.png" width="70" height="70"></p>
		<p class="paybill"><a <?php echo ' href="paybill.php?id='.$row['bill_id'].'"'; ?> >Pay Online</a></p>
		<p class="name"><?php echo $row['firstname'] . " " . $row['lastname'] ?></p>
	    <p class="date"><?php echo $row['create_time']?></p>
	    <p class="category">Category: <?php echo $row['name']?></p>
		<p class="notes">Notes: <?php echo $row['notes']?></p>
		<br>
	  <?php
	  } ?>
	  <p class="owe-sum">Total Owed: $<?php echo round($i_owe_sum['sum'], 1);?>.</p>
	  <br />
	  <br />
	  <br />
	  <hr class="hr-mybills">
	  <h3 class="title">I Paid: </h3>
	  <?php 
	  while ( $row = mysql_fetch_array( $people_owe ) ) {?>
		<p class="editbill"><a <?php echo ' href="editbill.php?id='.$row['bill_id'].'"'; ?> >Edit Bill</a></p>
		<?php
			$sql = "SELECT * FROM bills_users BU JOIN users U ON BU.user_id = U.id WHERE BU.bill_id = ".$row['bill_id']." AND if_paid != True";
			$results = mysql_query( $sql );
		?>
		<div class="pay-list">
			<p>Still Need to Pay:</p>
				<ul>
				<?php
					while ( $r = mysql_fetch_array( $results ) ) 
					{
							echo '<li>'.$r['lastname'].' '.$r['firstname'];
							echo '<a href="billpaid.php?bill_id='.$row['bill_id'].'&user_id='.$r['user_id'].'"> (paid)</a></li>';
					}
				?>
				</ul>
			</p>
		</div>
		<p class="amount-paid">$<?php echo round($row['amount'], 1)?></p>
		<p class="date">Date Created: <?php echo $row['create_time']?></p>
		<p class="category">Category: <?php echo $row['name']?></p>
		<p class="notes">Notes: <?php echo $row['notes']?></p>
		
		<br>
	  <?php
	  } ?>
	  <p class="paid-sum">Total Paid: $<?php echo round($i_paid_sum['sum'], 1);?>.</p>
	  <br />
	  <br />
	  <br />
	</div>
	</div>
</div>
 
<?php include('templates/main/footer.php'); ?>